package com.gree.flowable.app.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gree.flowable.app.mapper.FormDataExMapper;
import com.gree.flowable.app.mapper.FormModelMapper;
import com.gree.flowable.app.mapper.plus.FormDataExService;
import com.gree.flowable.app.pojo.form.FormModel;
import com.gree.flowable.app.pojo.model.FormDataEx;
import com.gree.flowable.app.pojo.vo.ReturnVO;
import com.gree.flowable.app.services.IFormDataExService;
import com.gree.flowable.app.tools.constants.ReturnCode;
import com.gree.flowable.app.tools.utils.DateType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/4/20 14:15
 * @description：
 * @modified By：
 * @version: 1.0
 */
@Service
public class IFormDataExServiceImpl implements IFormDataExService {

    @Autowired
    private FormDataExMapper formDataExMapper;
    @Autowired
    private FormDataExService formDataExService;

    @Autowired
    private FormModelMapper formModelMapper;

    @Override
    public ReturnVO<Boolean> addFormDataEx(String prid, String formId, Map<String, Object> formDataEx) {
        List<FormDataEx> formDataExList = new LinkedList<>();
        for (String key : formDataEx.keySet()) {
            FormDataEx dataEx = new FormDataEx();
            dataEx.setPrid(prid);
            dataEx.setFormId(formId);
            dataEx.setFields(key);
            dataEx.setValue(formDataEx.get(key).toString());
            formDataExList.add(dataEx);
        }
        boolean isAdd = formDataExService.saveBatch(formDataExList);
        ReturnVO<Boolean> result = null;
        if (isAdd){
            result = new ReturnVO<>(ReturnCode.SUCCESS,"数据插入成功",isAdd);
        } else {
            result = new ReturnVO<>(ReturnCode.FAIL,"数据插入失败",isAdd);

        }
        return result;
    }

    /**
     * 删除数据
     *
     * @param prid 流程实例id
     * @return 是否删除成功
     */
    @Override
    public ReturnVO<Integer> delFormDataEx(String prid) {
        QueryWrapper<FormDataEx> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FormDataEx::getPrid,prid);
        int total = formDataExMapper.delete(wrapper);
        ReturnVO<Integer> result= null;
        if (total > 0){
            result= new ReturnVO<>(ReturnCode.SUCCESS,"数据删除成功",total);
        } else {
            result = new ReturnVO<>(ReturnCode.FAIL,"数据删除失败",total);
        }
        return result;
    }

    /**
     * 更新数据
     *
     * @param prid 流程实例id
     * @param data 表单数据
     * @return
     */
    @Override
    public ReturnVO updateFormDataEx(String prid, String formId, Map<String, Object> data) {
        ReturnVO<Integer> delData = delFormDataEx(prid);
        ReturnVO<Boolean> addData = addFormDataEx(prid, formId, data);
        ReturnVO result = null;
        if ( delData.getData()> 0 && addData.getData()){
            result = new ReturnVO(ReturnCode.SUCCESS,"更新成功");
        } else {
            result = new ReturnVO(ReturnCode.FAIL,"更新失败");
        }
        return result;
    }

    /**
     * 查询明细表，将数据转为table显示
     *
     * @param prid   流程实例id
     * @param formId 表单id
     * @return
     */
    @Override
    public ReturnVO queryFormEx(String prid, String formId) {
        QueryWrapper<FormModel> formModelWrapper = new QueryWrapper<>();
        formModelWrapper.lambda().eq(FormModel::getFormId,formId);
        List<FormModel> formModelList = formModelMapper.selectList(formModelWrapper);
        Map<String,Map<String, String>> fieldMap = new HashMap<>();
        List<String> fieldList = new LinkedList<>();
        //组装表头
        for (FormModel formModel : formModelList) {
            fieldList.add(formModel.getFields());
            Map<String, String> fieldAttrMap = new HashMap<String, String>();
            fieldAttrMap.put("type",formModel.getShape());
            fieldAttrMap.put("label",formModel.getLabel());
            fieldAttrMap.put("sort",formModel.getSort().toString());
            fieldMap.put(formModel.getFields(),fieldAttrMap);
        }
        List<Map<String,Object>> valueList = new LinkedList<>();
        Map<String,Object> valueMap = new HashMap<>();
        //组装表内数据
        QueryWrapper<FormDataEx> formDataExWrapper = new QueryWrapper<>();
        formDataExWrapper.lambda().eq(FormDataEx::getPrid,prid).eq(FormDataEx::getFormId,formId);
        List<FormDataEx> formDataExList = formDataExMapper.selectList(formDataExWrapper);
        boolean dateType = true;
        for (FormDataEx formDataEx : formDataExList ) {

            for (String field : fieldList) {
                if (field.equals(formDataEx.getFields())){
                    String type = fieldMap.get(field).get("type");
                    for (DateType dateStr : DateType.values()) {
                        if (dateStr.getType().equals(type)){
                            valueMap.put(field,Integer.valueOf(formDataEx.getValue()));
                            dateType = false;
                            break;
                        }
                    }
                    if (!dateType){
                        valueMap.put(field,formDataEx.getValue());
                    }
                }
            }
            valueList.add(valueMap);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("fields",fieldMap);
        resultMap.put("field",fieldList);
        resultMap.put("values",valueList);
        ReturnVO result = null;
        if (resultMap.size() > 0){
            result = new ReturnVO(ReturnCode.SUCCESS,"查询成功",resultMap);
        } else {
            result = new ReturnVO(ReturnCode.FAIL,"查询失败",resultMap);
        }
        return result;
    }


}
